import * as types from './types'
import { parseLyric } from '@/utils/handleLyric.js'
import { getLyricString } from '@/api/index.js'
export default {
  sendAudio({ commit, state }, obj) {
    //添加音乐播放元素
    commit(types.SENDAUDIO, obj)
  },
  sendMusic({ commit, state }, obj) {
    //添加音乐
    console.log(obj.lyric)
    commit(types.SENDMUSIC, obj)
  },
  sendandchangeMusic({ commit, state }, obj) {
    //添加音乐并切换
    console.log(obj.lyric)
    commit(types.SENDANDCHANGE, obj)
  },
  play({ commit, state }) {
    //播放音乐

    if (state.playState == false) {
      state.dom.play()
      commit(types.PLAY)
    } else {
      state.dom.pause()
      commit(types.PAUSE)
    }
  },
  playMust({ commit, state }) {
    //播放音乐
    state.dom.play()
    commit(types.PLAY)
  },
  getcurrentTime({ state }) {
    state.currentTime = state.dom.currentTime
    // console.log(state.currentTime);
  },

  getDuration({ state }) {
    state.duration = state.dom.duration
  },
  closeListState({ commit, state }) {
    commit(types.CLOSELISTSTATE)
  },
  openListState({ commit, state }) {
    commit(types.OPENLISTSTATE)
  },

  selectMusic({ commit, state }, index) {
    commit(types.SELECTMUSIC, index)
  },
  delMusic({ commit, state }, index) {
    commit(types.DELMUSIC, index)
  },
  nextMusic({ commit, state }) {
    //播放下一曲
    state.musicList.length != 0 ? commit(types.LOOPPLAYBACK) : false
  },
  loopPlayback({ commit, state }) {
    //循环播放 自动下一首
    commit(types.LOOPPLAYBACK)
  },
  prevMusic({ commit, state }) {
    //播放上一曲
    state.musicList.length != 0 ? commit(types.PREVSONG) : false
  },
  setLyrics({ state }, obj) {
    return new Promise((resolve, reject) => {
      //调用后台接口获取歌词字符串
      getLyricString({ url: obj.lyric })
        .then((res) => {
          if (res.code === 200) {
            state.lyrics = res.data.lyric
            let lyrics = state.lyrics.split('\n')
            let lyricsResult = parseLyric(lyrics)
            // commit(types.SETLYRICSARR, lyricsResult)
            state.lyricsArr = lyricsResult
          } else {
            //获取歌词失败
            state.lyrics = ''
            obj.lyric = ''
            state.lyricsArr = []
          }
          resolve()
        })
        .catch((err) => {
          console.log(err)
          reject(err)
        })
    })
  },
  // setLyricsArr({ state }) {

  //   let lyrics = state.lyrics.split('\n')
  //   console.log('lyrics:', lyrics)
  //   let lyricsResult = parseLyric(lyrics)
  //   commit(types.SETLYRICSARR, lyricsResult)
  // },
  setCurrentTime({ commit, state }, time) {
    if (state.musicPlace != -1) {
      commit(types.SETCURRENTTIME, time)
    }
  },
  setUserInfo({ commit, state }, obj) {
    commit(types.SETUSERINFO, obj)
  },
  setNowSong({ commit, state }, obj) {
    commit(types.SETNOWSONG, obj)
  },
  changeIsThemeDark({ commit, state }, val) {
    commit(types.CHANGEISTHEMEDARK, val)
  },
  sendVolume({ commit, state }, val) {
    commit(types.SENDVOLUME, val)
  },
  sendSingerPoster({commit,state},obj){
    commit(types.SENDSINGERPOSTER,obj)
  },
  sendManageState({commit,state},val){
    commit(types.SENDMANAGESTATE,val)
  },
  changeBottomBarState({commit,state},val){
    commit(types.CHANGEBOTTOMBAR,val)
  },
  changePageGradation({commit,state},val){
    commit(types.CHANGEPAGEGRADATION,val)
  },
}
